package com.acce.gxsc.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by huxley on 2017/5/27.
 */

public class DateUtils {

    public static String getTime2() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
    }

    public static String toFormat(String startTime) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            Date parse = dateFormat.parse(startTime);
            return dateFormat1.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return startTime;
    }

    public static String getTime() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }

    public static String getTime(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }

    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    public static long getCurrentTimeMillis1() {
        return System.currentTimeMillis() + 24 * 60 * 60 * 1000;
    }

    public static String getTime1() {
        return new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
    }

    public static String getTime4() {
        return new SimpleDateFormat("MM-dd HH:mm:ss").format(new Date());
    }

    public static int[] getCurrentTimes() {
        Calendar calendar = Calendar.getInstance();
        int[] current = new int[3];
        current[0] = calendar.get(Calendar.HOUR_OF_DAY);
        current[1] = calendar.get(Calendar.MINUTE);
        return current;
    }

    public static int[] getCurrentDates(){
        Calendar calendar = Calendar.getInstance();
        int[] current = new int[3];
        current[0] = calendar.get(Calendar.YEAR);
        current[1] = calendar.get(Calendar.MONTH) + 1;
        current[2] = calendar.get(Calendar.DAY_OF_MONTH);
        return  current;
    }

    public static String minusDay(String date, String pattern) {
        return minusDate(Calendar.DAY_OF_MONTH, date, pattern);
    }

    public static String addDay(String date, String pattern) {
        return addDate(Calendar.DAY_OF_MONTH, date, pattern);
    }
    public static String addDate(int type, String date, String pattern) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(parseDate(date, pattern));
        gc.add(type, 1);
        return formatDate(gc.getTime(), pattern);
    }

    private static String formatDate(Date time, String pattern) {
        return new SimpleDateFormat(pattern).format(time);
    }

    private static Date parseDate(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }


    public static String minusDate(int type, String date, String pattern) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(parseDate(date, pattern));
        gc.add(type, -1);
        return formatDate(gc.getTime(), pattern);
    }

}
